#define __SDCC_FLOAT_LIB
#include <float.h>


#ifdef FLOAT_ASM_MCS51

static void dummy( void ) __naked {
  __asm
  .globl	___ulong2fs
  ___ulong2fs:
  mov	r4, a
  mov	r3, b
  mov	r2, dph
  mov	r1, dpl
  mov	a, #158
  .globl	ulong2fs_doit
  ulong2fs_doit:
  clr	sign_a
  long2fs_doit:
  mov	exp_a, a
  lcall	fs_normalize_a
  ljmp	fs_round_and_return
  __endasm;
}

#else

union float_long {
  float f;
  long l;
};

float __ulong2fs( unsigned long a ) {
  int exp = 24 + EXCESS;
  volatile union float_long fl;
  if( !a ) {
    return 0.0;
  }
  while( a & NORM ) {
    // we lose accuracy here
    a >>= 1;
    exp++;
  }
  while( a < HIDDEN ) {
    a <<= 1;
    exp--;
  }
  #if 1
  if( ( a & 0x7fffff ) == 0x7fffff ) {
    a = 0;
    exp++;
  }
  #endif
  a &= ~HIDDEN ;
  /* pack up and go home */
  fl.l = PACK( 0, ( unsigned long )exp, a );
  return ( fl.f );
}

#endif
